using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pipeline : MonoBehaviour
{

    public GameObject template;

    List<Pipe> pipes = new List<Pipe>();

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    Coroutine coroutine = null;

    public void Init()
    {
        for(int i = 0; i < pipes.Count; i++)
        {
            Destroy(pipes[i].gameObject);
        }
        pipes.Clear();
    }

    public void StartRun()
    {
        
            coroutine = StartCoroutine(GeneratePipelines());
       
       
    }

    public void Stop()
    {
        StopCoroutine(coroutine);
        for(int i= 0; i < pipes.Count; i++)
        {
            pipes[i].enabled = false;
        }
    }

    IEnumerator GeneratePipelines()
    {
        for (int i = 0; i < 3;i++)
        {
            if(pipes.Count <3)
            GeneratePipeline();
            else
            {
                pipes[i].enabled = true;
                pipes[i].Init();
            }
            yield return new WaitForSeconds(2f);
        }
    }

    void GeneratePipeline()
    {
        if (pipes.Count < 3)
        {
            GameObject obj = Instantiate(template, this.transform);
            Pipe p = obj.GetComponent<Pipe>();
            pipes.Add(p);
        }
    }

}
